﻿#ifndef __CODEC_H__
#define __CODEC_H__

#include "types.h"
#include <QTimer>
#include "databuffer.h"
#include "protocol/rxmessage.h"

#define CH_START						(u8)0xF5
#define CH_END							(u8)0xF6
#define CH_ESC							(u8)0xF7
#define CH_ESC_START					(u8)0x05
#define CH_ESC_END						(u8)0x06
#define CH_ESC_ESC						(u8)0x07

#define CODEC_MAX_RX_COUNT 1024

typedef void (*frame_handler_t)(uint8_t *data, int len);

class Decoder:public QObject,public DataBuffer {
    Q_OBJECT
public:
    explicit Decoder(int len);
    void append_data(u8 data);
    void append_data(u8* buff, int len);
    void clear(){
        DataBuffer::clear();
        //rx_length = 0;
        b_escape = false;
    }
signals:
    void on_frame(QByteArray &frame);
private:
    QTimer *frame_timer;
    //int  rx_length;
    bool b_escape;    
};

class Encoder:public DataBuffer {
public:
    explicit Encoder(int len = 1024);
    void AppendBytes(u8 *data, int len);
    void AppendValue8(u8 data);
    void AppendValue16(u16 data);
    void AppendValue24(u32 data);
    void AppendValue32(u32 data);
    void AppendFloat(float data);
    QByteArray get_buffer(){
        return Data;
    }
    u8 *get_data() {
        return (u8 *)Data.data();
    }
protected:
    void finish();
private:
    u16 crc;
};


#endif /* __CODEC_H__ */
